package com.ybbase.framework.controller.flow;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.RestController;
import com.ybbase.framework.base.controller.BaseController;
import com.ybbase.framework.service.flow.NodeConfigService;
import com.ybbase.framework.model.po.flow.NodeConfig;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.List;
import java.util.ArrayList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParams;
import com.ybbase.framework.base.model.ResponseBean;
/**
 * 节点配置表 前端控制器
 * @author cg
 * @since 2019-12-04
 * 云平台2.0
 */
@RestController
@RequestMapping("/node-config")
@Api("节点配置表")
public class NodeConfigController extends BaseController {
    private static final Logger LOGGER = LogManager.getLogger(NodeConfigController.class);

    @Autowired
    private NodeConfigService targetService;

    /**
     * 节点配置表分页查询
     * @param pageIndex 页数
     * @param step 每页数量
     * @return ResponseBean
     * @author cg
     * @date 2019-12-04
     * @since cloud2.0
     */
    @GetMapping("/list")
    @ApiOperation(value = "节点配置表分页查询", notes = "节点配置表分页查询", response = ResponseBean.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "rows", value = "每页行数", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "nodeconfig", value = "节点配置表", dataType = "NodeConfig对象")
    })
    public ResponseBean findListByPage(@RequestParam(name = "page", defaultValue = "1") int pageIndex,
                                       @RequestParam(name = "rows", defaultValue = "20") int step,NodeConfig nodeconfig){
        try{
            Wrapper<NodeConfig> wrapper = new QueryWrapper<NodeConfig>();
            Page<NodeConfig> page = new Page(pageIndex,step);
            targetService.page(page,wrapper);
            return new ResponseBean(9000,"查询成功!",page);
        }catch(Exception e){
            e.printStackTrace();
            LOGGER.error("节点配置表分页查询异常：", e.getMessage());
            return new ResponseBean(10000,"查询失败!",new Page());
        }
    }


    /**
     * 节点配置表获取全部数据
     * @return ResponseBean
     * @author cg
     * @date 2019-12-04
     * @since cloud2.0
     */
    @GetMapping("/all")
    @ApiOperation(value = "节点配置表获取全部数据", notes = "节点配置表获取全部数据")
    public ResponseBean findAll(){
        try{
            List<NodeConfig> models = targetService.list();
            return new ResponseBean(9000,"查询成功!",models);
        }catch(Exception e){
            e.printStackTrace();
            LOGGER.error("节点配置表获取全部数据异常：", e.getMessage());
            return new ResponseBean(10000,"查询失败!",new ArrayList<>());
        }
    }

    /**
     * 节点配置表根据ID查找数据
     * @param id 唯一编码
     * @return ResponseBean
     * @author cg
     * @date 2019-12-04
     * @since cloud2.0
     */
    @GetMapping("/find")
    @ApiOperation(value = "节点配置表根据ID查找数据", notes = "节点配置表根据ID查找数据")
    @ApiImplicitParam(required = true, name = "id", value = "唯一编码", dataType = "Long")
    public ResponseBean find(@RequestParam("id") Long id){
        try{
            NodeConfig nodeconfig = targetService.getById(id);
            if(nodeconfig==null){
                return new ResponseBean(10000,"查询无此记录!",null);
            }
            return new ResponseBean(9000,"查询成功!",nodeconfig);
        }catch(Exception e){
            e.printStackTrace();
            LOGGER.error("节点配置表根据ID查找数据异常：", e.getMessage());
            return new ResponseBean(10000,"查询失败!",new NodeConfig());
        }
    }

    /**
     * 节点配置表保存数据
     * @param nodeconfig  节点配置表
     * @return ResponseBean
     * @author cg
     * @date 2019-12-04
     * @since cloud2.0
     */
    @PostMapping("/save")
    @ApiOperation(value = "节点配置表保存数据", notes = "节点配置表保存数据")
    @ApiImplicitParam(required = true, name = "nodeconfig", value = "节点配置表", dataType = "NodeConfig对象")
    public ResponseBean save(@RequestBody NodeConfig nodeconfig){
        try{
            targetService.save(nodeconfig);
            return new ResponseBean(9000,"插入成功!",nodeconfig);
        }catch(Exception e){
            e.printStackTrace();
            LOGGER.error("节点配置表保存数据：", e.getMessage());
            return new ResponseBean(10000,"插入失败!",null);
        }
    }

    /**
     * 节点配置表更新数据
     * @param nodeconfig  节点配置表
     * @return ResponseBean
     * @author cg
     * @date 2019-12-04
     * @since cloud2.0
     */
    @PutMapping("/update")
    @ApiOperation(value = "节点配置表更新数据", notes = "节点配置表更新数据")
    @ApiImplicitParam(required = true, name = "nodeconfig", value = "节点配置表", dataType = "NodeConfig对象")
    public ResponseBean update(@RequestBody NodeConfig nodeconfig){
        try{
            targetService.updateById(nodeconfig);
            return new ResponseBean(9000,"更新成功!",nodeconfig);
        }catch(Exception e){
            e.printStackTrace();
            LOGGER.error("节点配置表更新数据：", e.getMessage());
            return new ResponseBean(10000,"更新失败!",null);
        }
    }

    /**
     * 节点配置表删除数据
     * @param ids 批量删除数据
     * @return ResponseBean
     * @author cg
     * @date 2019-12-04
     * @since cloud2.0
     */
    @DeleteMapping("/del")
    @ApiOperation(value = "节点配置表删除数据", notes = "节点配置表删除数据")
    @ApiImplicitParam(required = true, name = "ids", value = "批量删除数据", dataType = "List<Long>")
    public ResponseBean del(@RequestParam("ids") List<Long> ids){
        try{
            targetService.removeByIds(ids);
            return new ResponseBean(9000,"删除成功!",null);
        }catch(Exception e){
            e.printStackTrace();
            LOGGER.error("节点配置表删除数据：", e.getMessage());
            return new ResponseBean(10000,"删除失败!",null);
        }
    }
}
